package com.ihcil.forecast.demo.vo;

import com.ihcil.forecast.demo.utils.ValidatorUtil;
import com.ihcil.forecast.demo.validator.isMobile;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * Create by lotus on 2021/8/26 19:26
 *
 * 手机号码校验规则
 */


public class isMobileValidator implements ConstraintValidator<isMobile, String> {

    private boolean required = false;

    @Override
    public void initialize(isMobile constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String string, ConstraintValidatorContext constraintValidatorContext) {
        if (required) {
            return ValidatorUtil.isMobile(string);
        } else {
            if (StringUtils.isEmpty(string)) {
                return true;
            } else {
                return ValidatorUtil.isMobile(string);
            }
        }
    }
}
